package com.zwr.sheller.shop.controller;

import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.pojo.Discount;
import com.zwr.sheller.core.pojo.Gifts;
import com.zwr.sheller.core.pojo.Reduction;
import com.zwr.sheller.shop.service.DiscountService;
import com.zwr.sheller.shop.service.GiftsService;
import com.zwr.sheller.shop.service.ReductionService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 *
 *@author Kang
 *@date 
 */
@RestController
@RequestMapping("/discountController")
@Api(tags = "营销管理")
@CrossOrigin
public class DiscountController {


    /**
     * ---------------------------------------------------------------------------------
     * 满减策略
     * ---------------------------------------------------------------------------------
     */
    @Autowired
    ReductionService reductionService;

    @PostMapping("/addReduction")
    @ApiOperation("新建满减策略")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",dataType = "String",required = true,paramType = "query",value = "策略名称"),
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = true,paramType = "query",value = "满减类型 0：满金额减金额 1：满件数减金额"),
            @ApiImplicitParam(name = "mannerOne",dataType = "Double",required = true,paramType = "query",value = "满多少钱"),
            @ApiImplicitParam(name = "mannerTwo",dataType = "Double",required = true,paramType = "query",value = "减多少钱"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = true,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart",dataType = "String",required = false,paramType = "query",value = "开始时间"),
            @ApiImplicitParam(name = "timeOld",dataType = "String",required = false,paramType = "query",value = "结束时间")
    })
    public Map<String,Object> addReduction(String name,Boolean manner,Double mannerOne,Double mannerTwo,Boolean timePeriod,String timeStart,String timeOld){
       Reduction reduction=new Reduction();
       reduction.setName(name);
       reduction.setManner(manner);
       reduction.setMannerOne(mannerOne);
       reduction.setMannerTwo(mannerTwo);
       reduction.setTimePeriod(timePeriod);
       if (timePeriod==true){
           reduction.setTimeStart(Timestamp.valueOf(timeStart));
           reduction.setTimeOld(Timestamp.valueOf(timeOld));
       }
       reduction.setTimeNew(new Timestamp(System.currentTimeMillis()));

        Integer add = reductionService.add(reduction);
        System.out.println(reduction);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",add);
        return jsonResult.getMap();
    }

    @PostMapping("/selectByReductionPage")
    @ApiOperation("满减策略表格及分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = false,paramType = "query",value = "满减类型 0：满金额减金额 1：满件数减金额"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = false,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "name",dataType = "String",required = false,paramType = "query",value = "模糊查询"),
            @ApiImplicitParam(name = "page",dataType = "Integer",required = true,paramType = "query",value = "第几页")

    })
    public Map<String,Object> selectByReductionPage(Boolean manner,  Boolean timePeriod,String name,Integer page){
        Integer manner1=null;
        if (manner!=null){
            if (manner==true){
                manner1=1;
            }else {
                manner1=0;
            }
        }
        Integer timePeriod1 = null;
        if (timePeriod!=null){
            if (timePeriod==true){
                timePeriod1=1;
            }else {
                timePeriod1=0;
            }
        }


        List<Map<String, Object>> mapList = reductionService.selectByNameByTimeByManner(manner1, timePeriod1, name, page);
        Map map = reductionService.selectByNameByTimeByMannerCount(manner1, timePeriod1, name);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",mapList);
        jsonResult.setField("number",map);
        return jsonResult.getMap();
    }


    @PostMapping("/deleteByReduction")
    @ApiOperation("删除满减策略")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map",dataType = "Map",required = false,paramType = "query",value = "要删除的id")
    })
    public Map<String,Object> deleteByReduction(@RequestBody Map<String,Object> map){
        int i=0;
        System.out.println(map);
        List<Object> ids = (List<Object>) map.get("ids");
        for (Object id : ids) {
            Long idl=Long.parseLong(id.toString());
            System.out.println(idl);
            reductionService.delete(idl);
            i++;
        }

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("成功删除数据",i+"条");
        return jsonResult.getMap();
    }

    @PostMapping("/updateReduction")
    @ApiOperation("编辑满减策略")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",dataType = "Long",required = true,paramType = "query",value = "策略Id"),
            @ApiImplicitParam(name = "name",dataType = "String",required = false,paramType = "query",value = "策略名称"),
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = false,paramType = "query",value = "满减类型 0：满金额减金额 1：满件数减金额"),
            @ApiImplicitParam(name = "mannerOne",dataType = "Double",required = false,paramType = "query",value = "满多少钱"),
            @ApiImplicitParam(name = "mannerTwo",dataType = "Double",required = false,paramType = "query",value = "减多少钱"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = true,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart",dataType = "String",required = false,paramType = "query",value = "开始时间"),
            @ApiImplicitParam(name = "timeOld",dataType = "String",required = false,paramType = "query",value = "结束时间")
    })
    public Map<String,Object> updateReduction(Long id,String name,Boolean manner,Double mannerOne,Double mannerTwo,Boolean timePeriod,String timeStart,String timeOld){

        Reduction reduction=new Reduction();
        reduction.setId(id);
        reduction.setName(name);
        reduction.setManner(manner);
        reduction.setMannerOne(mannerOne);
        reduction.setMannerTwo(mannerTwo);
        reduction.setTimePeriod(timePeriod);
        if (timePeriod==true){
            reduction.setTimeStart(Timestamp.valueOf(timeStart));
            reduction.setTimeOld(Timestamp.valueOf(timeOld));
        }else {
            reduction.setTimeStart(null);
            reduction.setTimeOld(null);
        }
        Integer update = reductionService.update(reduction);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",update);
        return jsonResult.getMap();
    }



    /**
     * -------------------------------------------------------------------------------
     * 满赠操作
     * --------------------------------------------------------------------------------
     */
    @Autowired
    GiftsService giftsService;

    @PostMapping("/addGifts")
    @ApiOperation("新增满赠策略")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",dataType = "String",required = true,paramType = "query",value = "策略名称"),
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = true,paramType = "query",value = "满减类型 0：满金额送礼品 1：满件数送礼品"),
            @ApiImplicitParam(name = "mannerGifts",dataType = "Double",required = true,paramType = "query",value = "送礼品的策略"),
            @ApiImplicitParam(name = "mannerGoods",dataType = "String",required = true,paramType = "query",value = "送的礼品"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = true,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart",dataType = "String",required = false,paramType = "query",value = "开始时间"),
            @ApiImplicitParam(name = "timeOld",dataType = "String",required = false,paramType = "query",value = "结束时间")
    })
    public Map<String,Object> addGifts(String name,Boolean manner,Double mannerGifts,String mannerGoods,Boolean timePeriod,String timeStart,String timeOld){
        Gifts gifts=new Gifts();
        gifts.setName(name);
        gifts.setManner(manner);
        gifts.setMannerGifts(mannerGifts);
        gifts.setTimePeriod(timePeriod);
        gifts.setMannerGoods(mannerGoods);
        if (timePeriod==true){
            gifts.setTimeStart(Timestamp.valueOf(timeStart));
            gifts.setTimeOld(Timestamp.valueOf(timeOld));
        }
        gifts.setTimeNew(new Timestamp(System.currentTimeMillis()));
        Integer add = giftsService.add(gifts);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",add);
        return jsonResult.getMap();
    }

    @PostMapping("/selectByGiftsPage")
    @ApiOperation("满赠策略表格及分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = false,paramType = "query",value = "满减类型 0：满金额减金额 1：满件数减金额"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = false,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "name",dataType = "String",required = false,paramType = "query",value = "模糊查询"),
            @ApiImplicitParam(name = "page",dataType = "Integer",required = true,paramType = "query",value = "第几页")

    })
    public Map<String,Object> selectByGiftsPage(Boolean manner,  Boolean timePeriod,String name,Integer page){
        Integer manner1=null;
        if (manner!=null){
            if (manner==true){
                manner1=1;
            }else {
                manner1=0;
            }
        }
        Integer timePeriod1 = null;
        if (timePeriod!=null){
            if (timePeriod==true){
                timePeriod1=1;
            }else {
                timePeriod1=0;
            }
        }
        List<Map<String, Object>> mapList = giftsService.selectGiftsByNameByTimeByManner(manner1, timePeriod1, name, page);
        Map map = giftsService.selectGiftsByNameByTimeByMannerCount(manner1, timePeriod1, name);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",mapList);
        jsonResult.setField("num",map);
        return jsonResult.getMap();
    }

    @PostMapping("/deleteByGifts")
    @ApiOperation("删除满赠策略")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map",dataType = "Map",required = false,paramType = "query",value = "要删除的id")
    })
    public Map<String,Object> deleteByGifts(@RequestBody Map<String,Object> map){
        int i=0;
        System.out.println(map);
        List<Object> ids = (List<Object>) map.get("ids");
        for (Object id : ids) {
            Long idl=Long.parseLong(id.toString());
            System.out.println(idl);
            giftsService.delete(idl);
            i++;
        }

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("成功删除数据",i+"条");
        return jsonResult.getMap();
    }

    @PostMapping("/updateGifts")
    @ApiOperation("编辑满赠策略")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",dataType = "Long",required = true,paramType = "query",value = "策略id"),
            @ApiImplicitParam(name = "name",dataType = "String",required = false,paramType = "query",value = "策略名称"),
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = false,paramType = "query",value = "满赠类型 0：满金额送礼品 1：满件数送礼品"),
            @ApiImplicitParam(name = "mannerGifts",dataType = "Double",required = false,paramType = "query",value = "送礼品的策略"),
            @ApiImplicitParam(name = "mannerGoods",dataType = "String",required = true,paramType = "query",value = "送的礼品"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = true,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart",dataType = "String",required = false,paramType = "query",value = "开始时间"),
            @ApiImplicitParam(name = "timeOld",dataType = "String",required = false,paramType = "query",value = "结束时间")
    })
    public Map<String,Object> updateGifts(Long id,String name,Boolean manner,Double mannerGifts,String mannerGoods,Boolean timePeriod,String timeStart,String timeOld){

        Gifts gifts=new Gifts();
        gifts.setId(id);
        gifts.setName(name);
        gifts.setManner(manner);
        gifts.setMannerGifts(mannerGifts);
        gifts.setMannerGoods(mannerGoods);
        gifts.setTimePeriod(timePeriod);
        if (timePeriod==true){
            gifts.setTimeStart(Timestamp.valueOf(timeStart));
            gifts.setTimeOld(Timestamp.valueOf(timeOld));
        }else {
            gifts.setTimeStart(null);
            gifts.setTimeOld(null);
        }
        Integer update = giftsService.update(gifts);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",update);
        return jsonResult.getMap();
    }



    /**
     * -------------------------------------------------------------------------------
     * 折扣操作
     * --------------------------------------------------------------------------------
     */
    @Autowired
    DiscountService discountService;

    @PostMapping("/addDiscount")
    @ApiOperation("新增折扣策略")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",dataType = "String",required = true,paramType = "query",value = "策略名称"),
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = true,paramType = "query",value = "满减类型 0：满金额减折扣 1：满件折扣"),
            @ApiImplicitParam(name = "mannerOne",dataType = "Double",required = true,paramType = "query",value = "满多少钱"),
            @ApiImplicitParam(name = "mannerTwo",dataType = "Double",required = true,paramType = "query",value = "打几折"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = true,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart",dataType = "String",required = false,paramType = "query",value = "开始时间"),
            @ApiImplicitParam(name = "timeOld",dataType = "String",required = false,paramType = "query",value = "结束时间")
    })
    public Map<String,Object> addDiscount(String name,Boolean manner,Double mannerOne,Double mannerTwo,Boolean timePeriod,String timeStart,String timeOld){
        Discount discount=new Discount();
        discount.setName(name);
        discount.setManner(manner);
        discount.setMannerOne(mannerOne);
        discount.setMannerTwo(mannerTwo);
        discount.setTimePeriod(timePeriod);
        if (timePeriod==true){
            discount.setTimeStart(Timestamp.valueOf(timeStart));
            discount.setTimeOld(Timestamp.valueOf(timeOld));
        }
        discount.setTimeNew(new Timestamp(System.currentTimeMillis()));
        Integer add = discountService.add(discount);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",add);
        return jsonResult.getMap();
    }


    @PostMapping("/selectByDiscountPage")
    @ApiOperation("折扣策略表格及分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = false,paramType = "query",value = "满减类型 0：满金额减折扣 1：满件数折扣"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = false,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "name",dataType = "String",required = false,paramType = "query",value = "模糊查询"),
            @ApiImplicitParam(name = "page",dataType = "Integer",required = true,paramType = "query",value = "第几页")

    })
    public Map<String,Object> selectByDiscountPage(Boolean manner,  Boolean timePeriod,String name,Integer page){
        Integer manner1=null;
        if (manner!=null){
            if (manner==true){
                manner1=1;
            }else {
                manner1=0;
            }
        }
        Integer timePeriod1 = null;
        if (timePeriod!=null){
            if (timePeriod==true){
                timePeriod1=1;
            }else {
                timePeriod1=0;
            }
        }
        List<Map<String, Object>> mapList = discountService.selectDiscountByNameByTimeByManner(manner1, timePeriod1, name, page);
        Map map = discountService.selectDiscountByNameByTimeByMannerCount(manner1, timePeriod1, name);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",mapList);
        jsonResult.setField("num",map);
        return jsonResult.getMap();
    }


    @PostMapping("/deleteByDiscount")
    @ApiOperation("删除折扣策略")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map",dataType = "Map",required = false,paramType = "query",value = "要删除的id")
    })
    public Map<String,Object> deleteByDiscount(@RequestBody Map<String,Object> map){
        int i=0;
        System.out.println(map);
        List<Object> ids = (List<Object>) map.get("ids");
        for (Object id : ids) {
            Long idl=Long.parseLong(id.toString());
            System.out.println(idl);
            discountService.delete(idl);
            i++;
        }

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("成功删除数据",i+"条");
        return jsonResult.getMap();
    }


    @PostMapping("/updateDiscount")
    @ApiOperation("编辑折扣策略")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",dataType = "Long",required = true,paramType = "query",value = "策略id"),
            @ApiImplicitParam(name = "name",dataType = "String",required = false,paramType = "query",value = "策略名称"),
            @ApiImplicitParam(name = "manner",dataType = "Boolean",required = false,paramType = "query",value = "满减类型 0：满金额减折扣 1：满件折扣"),
            @ApiImplicitParam(name = "mannerOne",dataType = "Double",required = false,paramType = "query",value = "满多少钱"),
            @ApiImplicitParam(name = "mannerTwo",dataType = "Double",required = false,paramType = "query",value = "打几折"),
            @ApiImplicitParam(name = "timePeriod",dataType = "Boolean",required = true,paramType = "query",value = "是否开启时间1开启，0关闭"),
            @ApiImplicitParam(name = "timeStart",dataType = "String",required = false,paramType = "query",value = "开始时间"),
            @ApiImplicitParam(name = "timeOld",dataType = "String",required = false,paramType = "query",value = "结束时间")
    })
    public Map<String,Object> updateDiscount(Long id,String name,Boolean manner,Double mannerOne,Double mannerTwo,Boolean timePeriod,String timeStart,String timeOld){
        Discount discount=new Discount();
        discount.setId(id);
        discount.setName(name);
        discount.setManner(manner);
        discount.setMannerOne(mannerOne);
        discount.setMannerTwo(mannerTwo);
        discount.setTimePeriod(timePeriod);
        if (timePeriod==true){
            discount.setTimeStart(Timestamp.valueOf(timeStart));
            discount.setTimeOld(Timestamp.valueOf(timeOld));
        }else {
            discount.setTimeStart(null);
            discount.setTimeOld(null);
        }
        Integer update = discountService.update(discount);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data",update);
        return jsonResult.getMap();
    }
}
